package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Site_messageService struct{}

// CreateSite_message 创建站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) CreateSite_message(ctx context.Context, site_message *system.Site_message) (err error) {
	defaultUserId := 1
	defaultStatus := "2" // 2表示未读

	// 无论前端传什么都强制覆盖
	site_message.User_id = &defaultUserId
	site_message.Is_read = &defaultStatus

	err = global.GVA_DB.Create(site_message).Error
	return err
}

// DeleteSite_message 删除站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) DeleteSite_message(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Site_message{}, "id = ?", ID).Error
	return err
}

// DeleteSite_messageByIds 批量删除站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) DeleteSite_messageByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Site_message{}, "id in ?", IDs).Error
	return err
}

// UpdateSite_message 更新站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) UpdateSite_message(ctx context.Context, site_message system.Site_message) (err error) {
	err = global.GVA_DB.Model(&system.Site_message{}).Where("id = ?", site_message.ID).Updates(&site_message).Error
	return err
}

// GetSite_message 根据ID获取站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) GetSite_message(ctx context.Context, ID string) (site_message system.Site_message, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&site_message).Error
	return
}

// GetSite_messageInfoList 分页获取站内信记录
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) GetSite_messageInfoList(ctx context.Context, info systemReq.Site_messageSearch) (list []system.Site_message, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Site_message{})
	var site_messages []system.Site_message
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&site_messages).Error
	return site_messages, total, err
}

func (site_messageService *Site_messageService) GetSite_messagePublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

// GetAllMessages 分页获取所有消息
// Author [yourname](https://github.com/yourname)
func (s *Site_messageService) GetAllMessages(page, pageSize int, status, title string) ([]system.Site_message, int64, error) {
	var msgs []system.Site_message
	var total int64
	db := global.GVA_DB.Model(&system.Site_message{})

	// 添加搜索条件
	if status != "" {
		db = db.Where("is_read = ?", status)
	}
	if title != "" {
		db = db.Where("title LIKE ?", "%"+title+"%")
	}

	db.Count(&total)
	err := db.Order("created_at desc").Limit(pageSize).Offset((page - 1) * pageSize).Find(&msgs).Error
	return msgs, total, err
}

// MarkMessageRead 标记消息为已读
// Author [yourname](https://github.com/yourname)
func (s *Site_messageService) MarkMessageRead(msgId uint64) error {
	return global.GVA_DB.Model(&system.Site_message{}).Where("id = ?", msgId).Update("is_read", 1).Error
}

// BatchMarkRead 批量标记为已读
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) BatchMarkRead(ids []uint64) (err error) {
	return global.GVA_DB.Model(&system.Site_message{}).
		Where("id IN ?", ids).
		Update("is_read", 1).Error
}

// BatchDelete 批量删除
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) BatchDelete(ids []uint64) (err error) {
	return global.GVA_DB.Where("id IN ?", ids).
		Delete(&system.Site_message{}).Error
}

// AllMarkRead 全部标记为已读
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) AllMarkRead() (err error) {
	return global.GVA_DB.Model(&system.Site_message{}).
		Update("is_read", 1).Error
}

// AllDelete 全部删除
// Author [yourname](https://github.com/yourname)
func (site_messageService *Site_messageService) AllDelete() (err error) {
	return global.GVA_DB.Delete(&system.Site_message{}).Error
}
